<?php

require FCPATH . 'application/controllers/backend/backend.php';

class Label extends Backend {

    public function __construct() {
        parent::__construct();

        $this->load->model('label_model', 'model');
    }

    public function index() {
        $this->load_javascripts('backend_label');

        $this->load_stylesheets(array(
            'global',
            'backend_form'
        ));

        $data = array(
            'parents' => $this->model->records(0, 0, array(array('parent', 0)))
        );

        $this->rend('label_index_view', $data);
    }

    public function find() {
        $id = intval($this->uri->segment(4));

        $data = $this->model->find($id);

        if ($data) {
            $this->json_back('XT001', '', $data);
        } else {
            $this->json_back('XT002', '数据库错误');
        }
    }

    public function records() {
        $page = intval($this->input->get('page'));
        $page_size = intval($this->input->get('pageSize'));
        $page = $page ? $page : 1;
        $page_size = $page_size ? $page_size : 20;

        $offset = ($page - 1) * $page_size;

        $conditions = array(array('parent', intval($this->input->get('parent'))));

        $this->json_back('XT001', '', array(
            'total' => $this->model->total($conditions),
            'records' => $this->model->records($page_size, $offset, $conditions)
        ));
    }

    public function insert() {
        $data = array(
            'parent' => intval($this->input->post('parent')),
            'value' => trim($this->input->post('value')),
            'value_en' => trim($this->input->post('value_en'))
        );

        if (empty($data['value'])) {
            $this->json_back('XT002', '值不能为空');
        }

        if (empty($data['value_en'])) {
            $this->json_back('XT002', '英文值不能为空');
        }

        $conditions = array(
            array('value', $data['value']),
            array('parent', $data['parent'])
        );

        if ($this->model->total($conditions) > 0) {
            $this->json_back('XT002', '已经存在，请更换一个');
        }

        $data['order'] = $this->model->next_order($data['parent']);

        if ($this->model->insert($data)) {
            $this->json_back('XT001');
        } else {
            $this->json_back('XT002', 'database error.');
        }
    }

    public function update() {
        $id = intval($this->input->post('id'));

        $data = array(
            'value' => trim($this->input->post('value')),
            'value_en' => trim($this->input->post('value_en'))
        );

        if (empty($data['value'])) {
            $this->json_back('XT002', '值不能为空');
        }

        if (empty($data['value_en'])) {
            $this->json_back('XT002', '英文值不能为空');
        }

        if (!($label = $this->model->find($id))) {
            $this->json_back('XT002', '已被删除');
        }

        $conditions = array(
            array('value', $data['value']),
            array('id <>', $id),
            array('parent', $label->parent)
        );

        if ($this->model->total($conditions) > 0) {
            $this->json_back('XT002', '已经存在，请更换一个');
        }

        if ($this->model->update($id, $data)) {
            $this->json_back('XT001');
        } else {
            $this->json_back('XT002', 'database error.');
        }
    }

    public function order() {
        $id = intval($this->input->post('id'));

        $order = intval($this->uri->post('order'));

        if (!($this->model->find($id))) {
            $this->json_back('XT002', '已被删除');
        }

        if ($this->model->order($id, $order)) {
            $this->json_back('XT001', '排序成功');
        } else {
            $this->json_back('XT002', '数据库错误');
        }
    }

    public function order_move() {
        $id = intval($this->uri->segment(4));
        $offset = intval($this->uri->segment(5));

        if (!($label = $this->model->find($id))) {
            $this->json_back('XT002', '已被删除');
        }

        $order = intval($label->order) + $offset;

        if ($this->model->order($id, $order)) {
            $this->json_back('XT001');
        } else {
            $this->json_back('XT002', '数据库错误');
        }
    }

    public function is_empty() {
        $id = intval($this->uri->segment(4));

        if (!$this->model->find($id)) {
            $this->json_back('XT002', '记录不存在，已被删除');
        }

        $total = $this->model->total(array(array('parent', $id)));

        $this->json_back('XT001', '', $total);
    }

    public function delete() {
        $id = intval($this->uri->segment(4));

        if (!$this->model->find($id)) {
            $this->json_back('XT002', '记录不存在，已被删除');
        }

        if ($this->model->total(array(array('parent', $id))) > 0) {
            $this->model->delete_where(array(array('parent', $id)));
        }

        if ($this->model->delete($id)) {
            $this->json_back('XT001');
        } else {
            $this->json_back('XT002', 'database error.');
        }
    }

}
